package com.itheima.ssm.controller;

import com.itheima.ssm.domain.Role;
import com.itheima.ssm.service.IRoleService;
import org.apache.ibatis.annotations.Update;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping("/role")
public class RoleController {
    @Autowired
    private IRoleService roleService;


    @RequestMapping("/findAll")
    public ModelAndView findAll(){
        ModelAndView mv= new ModelAndView();
        List<Role> all = roleService.findAll();
        System.out.println(all);
        mv.addObject("roleList",all);
        mv.setViewName("role-list");

        return  mv;
    }

    @RequestMapping("save")
    public String save(Role role){
        roleService.save(role);
        return "redirect:findAll";
    }


    @RequestMapping("/find")
    public ModelAndView findAllRole(){
        ModelAndView mv= new ModelAndView();
        List<Role> all = roleService.findAll();
        System.out.println(all);
        mv.addObject("roleList",all);
        mv.setViewName("user-add");

        return  mv;
    }

@RequestMapping("/findRoles")
    public ModelAndView finRoleByUserId(String uid){
    ModelAndView mv= new ModelAndView();
    List<Role> roleList = roleService.findByUserId(uid);
    System.out.println(roleList);
    mv.addObject("roleList",roleList);
    mv.addObject("uId", uid);
    mv.setViewName("user-role-add");

    return mv;
}

}
